"use strict";

angular.module('app')
	.directive('fileModel', [function() {
		return {
			restrict: 'EA',
			scope: {
				setFileData: '&'
			},
			link: function(scope, elem, attrs) {
				elem.on('change', function() {
					scope.$apply(function() {
						var val = elem[0].files[0];
						scope.setFileData({ value: val });
					});
				});
			}
		};
	}])
	.directive('hasAuthority', ['authorities', function(authorities) {
		return {
			link: function(scope, element, attrs) {
				if (typeof (attrs.hasAuthority) !== 'string') {
					throw "hasAuthority must have string value";
				}
                var values = attrs.hasAuthority.split(",");
                var last = false;
				for (var i in values) {
                    var value = values[i];
                    var noAuthorityFlag = value[0] === '!';
                    if (noAuthorityFlag) value = value.slice(1).trim();

                    var authorized = authorities.hasAuthority(value);
                    if (authorized) {
                        if (!noAuthorityFlag) last = true;
                    } else {
                        if (noAuthorityFlag) last = true;
                    }
                }
                if (!last) element.remove();
			}
		};
	}])
	.directive('hasRole', ['authorities', function(authorities) {
		return {
			link: function(scope, element, attrs) {
				if (typeof (attrs.hasRole) !== 'string') {
					throw "hasRole must have string value";
				}
				var value = attrs.hasRole.trim();
				// var noRoleFlag = value[0] === '!';
				// if (noRoleFlag) value = value.slice(1).trim();
				var checkRole = function() {
					var valueList = value.split(',');
					var authorized = false;
					for (var idx in valueList) {
						var val = valueList[idx].trim();
						if (val.length > 0) {
							if (authorities.hasRole(val)) authorized = true;
						}
					}
					if (!authorized) element.remove();
				};
				checkRole();
			}
		};
	}])
	// .directive('terraceTemplate', ['$location',function($location) {
	// 	return {
	// 		restrict: 'E',
	// 		link: function (scope, element, attrs) {
	// 			var cssName = attrs.terraceTemplate.trim();
	// 			var hostName = $location.host();
	// 			//hostName = "hansap.com";
	// 			var checked = /^w{3}.*com$/i;
	// 			var checked2 = /^.*com$/i;
	// 			var domainName = "";
	// 			if(hostName.match(checked)) {
	// 				domainName = hostName.split('.')[1];
	// 			} else if(hostName.match(checked2)){
	// 				domainName = hostName.split('.')[0];
	// 			}
	// 			scope.updateCss = function (domainName) {
	// 				if (domainName != '') {
	// 					if (domainName == 'hansap') {
	// 						$(element).attr("href", "css/" + cssName + ".css");
	// 					} else {
	// 						$(element).attr("href", domainName + "/" + "css/" + cssName + ".css");
	// 					}
	// 				} else {
	// 					$(element).attr("href", "css/" + cssName + ".css");
	// 				}
	// 			};
	// 			scope.updateCss(domainName);
	// 		}
	// 	};
	// }])
	.directive('pageFooter', function () {
		return {
			restrict: 'E',
			transclude: true,
			replace: true,
			templateUrl: 'template/blocks/page_footer.html?v=351d3bf357'
		};
	});
